package com.xvyy.tingshu.order.api;

import com.xvyy.tingshu.common.login.annotation.TsLogin;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.model.order.OrderInfo;
import com.xvyy.tingshu.order.service.OrderInfoService;
import com.xvyy.tingshu.vo.order.OrderInfoVo;
import com.xvyy.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    // http://192.168.217.1:8500/api/order/orderInfo/trade
    @PostMapping("/trade")
    @Operation(summary = "订单结算页面")
    @TsLogin
    public Result trade(@RequestBody @Validated  TradeVo tradeVo) {
        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo);
        List<Object> exitItemIdList = orderInfoVo.getExitItemIdList();
        if (!CollectionUtils.isEmpty(exitItemIdList)) {
            return Result.build(exitItemIdList, 301, "付款项已经存在");
        }
        return Result.ok(orderInfoVo);
    }

    // http://192.168.217.1:8500/api/order/orderInfo/submitOrder
    @PostMapping("/submitOrder")
    @Operation(summary = "提交订单")
    @TsLogin
    public Result<Map<String, Object>> submitOrder(@RequestBody  @Validated OrderInfoVo orderInfoVo) {
        Map<String, Object> resultMap = orderInfoService.submitOrder(orderInfoVo);
        return Result.ok(resultMap);
    }

    // http://192.168.217.1:8500/api/order/orderInfo/getOrderInfo/undefined
    @GetMapping("/getOrderInfo/{orderNo}")
    @Operation(summary = "根据订单编号获取订单详情")
    @TsLogin
    public Result<OrderInfo> getOrderInfo(@PathVariable(value = "orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }

    // http://192.168.217.1:8500/api/order/orderInfo/findUserPage/1/10
    @GetMapping("/findUserPage/{pn}/{pz}")
    @Operation(summary = "分页查询用户订单列表")
    @TsLogin
    public Result findUserPage(@PathVariable(value = "pn") Long pn, @PathVariable(value = "pz") Long pz) {
        IPage<OrderInfo> page = new Page<>(pn, pz);
        page = orderInfoService.findUserPage(page);
        return Result.ok(page);
    }
}

